from qiniu import Auth, put_file
from qiniu import BucketManager

qiniuAccessKey = "Uc_OzJKLI2Nk2wk7zFIE36_Wtv4iNL5lTqkiN8Zq"
qiniuSecretKey = "AodDADJV4kCP5KIOrxEyHxah2DmGEhmP9L3dVgdr"
bucketName = "expressionpro"
qiniuAuth = Auth(access_key=qiniuAccessKey, secret_key=qiniuSecretKey)


def upload_file(localfile, keyName):
    global qiniuAccessKey, qiniuSecretKey, bucketName, qiniuAuth
    """
    上传文件
    :param data: 要上传的bytes类型数据
    :return:
    """
    # 构建鉴权对象
    # 生成上传 Token，可以指定过期时间等
    token = qiniuAuth.upload_token(bucketName, keyName, 60)
    ret, info = put_file(token, keyName, localfile, version='v2')
    # print(info)
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)

    if info.status_code != 200:
        print("上传错误")
    return ret, info


def deleteAfterDays(key, days):
    """设置文件延迟删除时间"""
    global qiniuAccessKey, qiniuSecretKey, bucketName, qiniuAuth
    # 初始化BucketManager
    bucket = BucketManager(qiniuAuth)
    # 您要更新的生命周期
    ret, info = bucket.delete_after_days(bucketName, key, str(days))
    print(info)


if __name__ == '__main__':
    upload_file(r"N:\trainData\image\900030fa833e44fcaefbc7c24e5fb7c0.jpeg",
                "900030fa833e44fcaefbc7c24e5fb7c0.jpeg")
